<template>
    <el-dialog 
      :title="userId ? '修改课题' :  '新增课题'"
      :close-on-press-escape="false" 
      :close-on-click-modal="false"
      :append-to-body="true"
      :lock-scroll="true"
      :visible.sync="visible">
      <el-form ref="dataForm" :model="dataForm" label-width="120px">
        <el-form-item label="课题名" required >
          <el-input v-model="dataForm.cgyName" show-word-limit maxlength="20"></el-input>
        </el-form-item>
        <el-form-item label="描述" required>
          <el-input type="textarea" v-model="dataForm.describes" show-word-limit maxlength="400"></el-input>
        </el-form-item>
        
        <el-form-item label="课题类别" required>
          <el-select filterable v-model="dataForm.cgyType" placeholder="类别">
            <el-option label="类别1" value="类别1"></el-option>
            <el-option label="类别2" value="类别2"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="需求人数" required>
          <el-input v-model="dataForm.score_db"></el-input>
        </el-form-item> -->
        
        <!-- <el-form-item label="班级" prop="" size="mini">
          <el-select filterable v-model="dataForm.stuClass" placeholder="班级">
            <el-option label="2020级3班" value="202003"></el-option>
            <el-option label="2020级4班" value="202004"></el-option>
            <el-option v-for="item in classlist" :lable="item.lable" :value="item.value"></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="状态" size="mini" prop="state">
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
          <el-radio-group v-model="dataForm.state" disabled>
            <el-radio v-for="item in stateList" :key="item.state" :label="item.state">{{ item.stateName }}</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form> 
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span> 
    </el-dialog>
  </template>
    
  <script>
  import servicesApi from '@/services/servicesApi'
//   import { getDictDataList } from '@/utils';
  export default {
    data() {
      return {
        dataForm: {
          cgyId: 0,
          userId: 0,
          cgyName: null,
          cgyDepart: null,
          cgyType: null,
          cgySource: null,
          tusername: null,
          teaName: null,
          describes: null,
          cgyStatus: 2,
          cgyLeadstatus: 2,
          cgyLeadadvice: null,
          cgyAdvise: null,
          gmtCreate: '2022-11-18 21:24:46',
          gmtModified: '2022-11-24 21:35:32',
          version: 7,
          deleted: 0
        },
        visible: false,
        userId: 0,
        cgyName: '',
      }
    },
    methods: {
      getDictList(dictType) {
        let list = getDictDataList(dictType)
        // // console.log(list)
        return list
      },
      // 显示弹窗
      init(userId, cgyName) {
        this.visible = true
        // console.log(userId, cgyName)
        this.userId = userId || 0
        this.cgyName = cgyName || ''
        if (this.userId) { //传入userID->修改
          // console.log('修改课题')
          servicesApi.getTdtopic(this.userId).then(res => {
            let code = res.data.code
            let data = res.data.data
            if (code === 200) {
              // console.log(data)
              // this.dataForm = { state: 1, ...data }
              this.dataForm = data
            }
          })
        } else { //新增
          this.dataForm = {}
          // console.log('新增课题')
        }
      },
      // 提交表单
      dataFormSubmit() {
        // this.$message({
        //   message: '操作成功',
        //   type: 'success',
        //   duration: 1500,
        //   onClose: () => {
        //     this.visible = false
        //     this.$emit('refreshDataList')
        //   }
        // })
        if (this.userId) {//修改
          servicesApi.changeTtopic(this.dataForm).then(res => {
            let code = res.data.code
            if (code == 200) {
              this.$message({
                message: '修改成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.data.msg)
            }
          })
        } else { //新增
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              servicesApi.submitTtopic(this.dataForm).then(res => {
                if (res.data.code === 200) {
                  this.$message({
                    message: '新增成功',
                    type: 'success',
                    duration: 500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList') //回显
                    }
                  })
                } else {
                  this.$message.error(res.data.msg)
                }
              })
            }
          })
        }
  
      }
    },
  }
  </script>
    